import GCJ_CLASS_TYPE = require('./../../common/GCJ_CLASS_TYPE');
import Point = require('./../../common/graph/Point');
import Serializer = require('./Serializer')

export  = ObjPosition;
    
    
/**
 *
 * @author 
 *
 */
class ObjPosition extends Serializer{
    public x = 0; 
    public y = 0; 
    public w = 0;
    public h = 0;
    public an = 0; // angle角度 [-180,180]
    
    public constructor(x?,y?,w?,h?,an?) {
        super();
        this.ct = GCJ_CLASS_TYPE.CLASS_TYPE_ObjPosition;
        
        if(x){
            this.x=x;
        }
        if(y){
            this.y=y;
        }
        if(w){
            this.w=w;
        }
        if(h){
            this.h=h;
        }
        if(an){
            this.an;
        }
    }
    
    // 拷贝
    public clone():ObjPosition {
        var ret: ObjPosition=new ObjPosition();
        ret.copy(this);
        return ret;
    }
    public copy(from:ObjPosition){
        this.fromJson(JSON.parse( from.toJson()), 0 );
    }
    public copyFromShape(from){// from:egret.Shape
        this.x = from.x;
        this.y = from.y;
        this.w = from.width;
        this.h = from.height;
        this.an = from.rotation;
    }
    // 合并
    public union(from:ObjPosition){
        var this_max_x: number = this.x + this.w;
        var this_max_y: number = this.y + this.h;
        var from_max_x: number = from.x + from.w;
        var from_max_y: number = from.y + from.h;
        var max_x = this_max_x > from_max_x ? this_max_x : from_max_x;
        var max_y = this_max_y > from_max_y ? this_max_y : from_max_y;
        
        this.x = this.x < from.x ? this.x : from.x;
        this.y = this.y < from.y ? this.y : from.y;
        this.w = max_x - this.x;
        this.h = max_y - this.y;
        
    }
    // 乘法
    public multiply(ratio:number){
        this.x *= ratio;
        this.y *= ratio;
        this.w *= ratio;
        this.h *= ratio;
        
    }
    // 将矩形（w，h）进行保持宽高比的缩放，撑满到当前窗口ObjPosition，输出新ObjPosition
    public adjust(w:number, h:number):ObjPosition{
        
        
        var newW=w,newH=h,newX=0,newY=0;
        if(w/h>this.w/this.h) {
            newH=this.h;
            newW=this.h*w/h;
            newX=-(newW-this.w)/2;
            newY=0;
        } else { 
            newW = this.w;
            newH=this.w*h/w;
            newX=0;
            newY=-(newH-this.h)/2;
        }
        
        var ret: ObjPosition=new ObjPosition(newX,newY,newW,newH);
        return ret;
    }
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj, versionId) { 
        super.fromJson(jsonObj,versionId);
                                
        this.x=Point.removeZero(jsonObj.x) ;
        this.y=Point.removeZero(jsonObj.y);
        this.an = jsonObj.an;
        this.w=Point.removeZero(jsonObj.w);
        this.h=Point.removeZero(jsonObj.h);
                
    }
    // 转成json字符串
    public toJson():string {
        // 去掉父json字符串的头尾
        var superJson = super.toJson();
        superJson = superJson.substr(1,superJson.length-2);
                                                
        var ret = '{';
            ret +=superJson;
                                                                                
            // x
            ret +=',';
            ret += '"x":';
            ret+=Point.removeZero(this.x);
            // y
            ret +=',';
            ret += '"y":';
            ret+=Point.removeZero(this.y);
            // an
            ret +=',';
            ret += '"an":';
            ret += this.an;
            // w
            ret +=',';
            ret += '"w":';
            ret+=Point.removeZero(this.w);
            // h
            ret +=',';
            ret += '"h":';
            ret+=Point.removeZero(this.h);
                                                                    
                                                                                
            ret+='}';
            return ret;
    }  
    
    
        
//SJG_MARK_APPEND--------------------


}//SJG_MARK_END-------------------------